#include <stdio.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <linux/if_ether.h>
#include <linux/in.h>
#include <unistd.h>
#include <pthread.h>
#include "duoxiancheng.h"

bao *fgs;
FILE *fg=NULL,*in=NULL;
int sum=0,code_sum = 0,th=1,namelen = -1;
long size;
pthread_mutex_t mutex,mutexr,du_mutex,xie_mutex;//PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond,xcond; 
haf arr[256] = {0};
char *code[256] = {0},**outname,*inname = NULL;
unsigned char ***code_tab;
long_code ***lcode_tab;

void caozuo(void* (*pfile)(fhanshu *),fhanshu *fhan){
	pthread_t id1;
	int i=0,cpu=0;
	FILE *fp;
	char buf;
	if((fp = popen("lscpu | grep ^CPU\\:| tr -s ' ' | cut -d' ' -f 2","r"))==NULL){
		perror("命令执行错误");
		exit(1);
	}
	if(fread(&buf,sizeof(char),1,fp)!=1){
		perror("读取错误");
		exit(1);
	}
	pclose(fp);
	cpu = atoi(&buf);
	if(th == 1){
		cpu*=2;
	}else{
		cpu = 1;
	}
	pthread_t id[cpu];
	while(i<cpu){
		if((pthread_create(&id1,NULL,(void*)pfile,fhan))){
			perror("线程创建失败");
			exit(1);
		}
		id[i] = id1;
		++i;
	}
	for(int a=0;a<cpu;++a){
		pthread_join(id[a],NULL);
	}
}

void* fileduqu(fhanshu *fhan){
	unsigned char buf[4096];
	long c=0;
	int b;
	FILE *fg = fhan->fp;
	while(1){
		b=0;
		c = size-ftell(fg);	
		if(c < 4096 && c > 0){
			fread(buf,c,1,fg);
			b=c;
		}else if(c >= 4096){
			fread(buf,4096,1,fg);
			b=4096;
		}else{
			if(feof(fg)){
				break;
			}
			fread(buf,1,1,fg);
		}
		fhan->hp(buf,b,size-ftell(fg),fhan->ofp);
		}
	return (void *)0;
}

void* duqu(fhanshu *fhan){
	unsigned char buf[4096];
	long c=0;
	int b;
	FILE *fg = fhan->fp;
	while(1){
		b=0;
		if(pthread_mutex_lock(&mutexr)!=0){
			perror("互斥锁出问题");
			exit(1);	
		}	
		c = size-ftell(fg);	
		if(c < 4096 && c > 0){
			fread(buf,c,1,fg);
			b=c;
		}else if(c >= 4096){
			fread(buf,4096,1,fg);
			b=4096;
		}else{
			if(feof(fg)){
				if(pthread_mutex_unlock(&mutexr)!=0){
					perror("互斥锁出问题");
					exit(1);
				}
				break;
			}
			fread(buf,1,1,fg);
		}
		if(pthread_mutex_unlock(&mutexr)!=0){
			perror("互斥锁出问题");
			exit(1);
		}		
		fhan->hp(buf,b,size-ftell(fg),fhan->ofp);
		}
	return (void *)0;
}

void* pthduqu(fhanshu *fhan){
	unsigned char buf[4096];
	long c=0;
	int b;
	long len;
	FILE *fgg;
	bao bp;
las:
	while(namelen>0){
		if(pthread_mutex_lock(&mutexr)!=0){
			perror("互斥锁出问题");
			exit(1);	
		}
		while(namelen<=0){
			goto las;
		}
		bp = fgs[namelen-1];
		--namelen;
		if(pthread_mutex_unlock(&mutexr)!=0){
			perror("互斥锁出问题");
			exit(1);
		}	
		fgg = bp.fg;
		len = bp.len;
		sy s = {0};
		while(1){
			b=0;
			c= len-ftell(fgg);
			if(c < 4096 && c > 0){
				fread(buf,c,1,fgg);
				b=c;
			}else if(c >= 4096){
				fread(buf,4096,1,fgg);
				b=4096;
			}else{
				if(feof(fgg)){
					goto las;
				}				
				fread(buf,1,1,fgg);
			}
			s = fhan->iohp(s,buf,b,len-ftell(fgg),bp.ofg);
		}

	}
	return (void *)0;
}

void* pinduqu(fhanshu *fhan){
	unsigned char buf[4096];
	long c=0;
	int b;
	long len;
	FILE *fgg;
	bao bp;
	sy s = {0};
las:
	while(namelen>0){
		if(pthread_mutex_lock(&mutexr)!=0){
			perror("互斥锁出问题");
			exit(1);	
		}
		while(namelen<=0){
			goto las;
		}
		bp = fgs[namelen-1];
		--namelen;
		if(pthread_mutex_unlock(&mutexr)!=0){
			perror("互斥锁出问题");
			exit(1);
		}	
		fgg = bp.fg;
		len = bp.len;
		while(1){
			b=0;
			c= len-(ftell(fgg)-bp.start);
			
			if(c < 4096 && c > 0){
				fread(buf,c,1,fgg);
				b=c;
			}else if(c >= 4096){
				fread(buf,4096,1,fgg);
				b=4096;
			}else{
				if(feof(fgg)){
					goto las;
				}
				else if(c==0){
					goto las;
				}
				fread(buf,1,1,fgg);
			}
			s = fhan->iohp(s,buf,b,len-(ftell(fgg)-bp.start),bp.ofg);
		}

	}
	return (void *)0;
}

